﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ServiceModel;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;

//using ColourAdvisorPhoneApp.ColourAdvisorServiceReference; // local host service reference
using ColourAdvisorPhoneApp.FashionColourAdvisorCloudServiceReference; // Azure cloud service reference


namespace ColourAdvisorPhoneApp
{
    public partial class ColouringDetailsPage : PhoneApplicationPage
    {
        // private fields
        private string hairColour;
        private string eyeColour;
        private string skinTone;

        // read only property for Hair Colour
        public string HairColour
        {
            get
            {
                return hairColour;
            }
        }

        // read only property for Eye Colour
        public string EyeColour
        {
            get
            {
                return eyeColour;
            }
        }

        // read only property for Skin Tone
        public string SkinTone
        {
            get
            {
                return skinTone;
            }
        }
        // lists of hair colours, eye colours, skin tones for user to select from
        // static because shared between all users
        private static List<string> hairColourOptions;
        private static List<string> eyeColourOptions;
        private static List<string> skinToneOptions;

        //public read only property
        public static List<string> HairColourOptions
        {
            get
            {
                return hairColourOptions;
            }
        }

        //public read only property
        public static List<string> EyeColourOptions
        {
            get
            {
                return eyeColourOptions;
            }
        }

        //public read only property
        public static List<string> SkinToneOptions
        {
            get
            {
                return skinToneOptions;
            }
        }

        public ColouringDetailsPage()
        {
            InitializeComponent();
            
            // initialize the lists
            hairColourOptions = new List<string> { "Ash Blonde", "Golden Blonde", "Strawberry Blonde", "Light Brown", "Medium Brown", "Ash Brown", "Dark Brown", "Black", "Red", "Auburn", "Dark Auburn" };
            eyeColourOptions = new List<string> { "Bright Blue", "Blue", "Dark Blue", "Blue/Grey", "Bright Blue/Green", "Blue/Green", "Bright Green", "Green", "Bright Hazel", "Hazel", "Brown", "Dark Brown", "Steel Grey" };
            skinToneOptions = new List<string> { "Light: Fair, Porcelain, Light Beige", "Medium: Beige to Sallow, Olive", "Dark: Olive to Brown, Black" };

            // bind hair colour list data to the List Box
            hairColourListBox.ItemsSource = HairColourOptions;
            // set selected index to 0 so ListBox has a valid SelectedItem at startup
            hairColourListBox.SelectedIndex = 0;

            // bind eye colour list data to the List Box
            eyeColourListBox.ItemsSource = EyeColourOptions;
            // set selected index to 0 so ListBox has a valid SelectedItem at startup
            eyeColourListBox.SelectedIndex = 0;

            // bind skin tone list data to the List Box
            skinToneListBox.ItemsSource = SkinToneOptions;
            // set selected index to 0 so ListBox has a valid SelectedItem at startup
            skinToneListBox.SelectedIndex = 0;
        }

        private void GoButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // use .SelectedItem on the ListBoxes to retrieve the selected values and use ToString to return as a string
                hairColour = hairColourListBox.SelectedItem.ToString();
                eyeColour = eyeColourListBox.SelectedItem.ToString();
                skinTone = skinToneListBox.SelectedItem.ToString();

                // web service method takes 3 int type parameters 
                // use .IndexOf to get index value of the selected hair and eye colour and skin tone
                int hairIndex = HairColourOptions.IndexOf(hairColour);
                int eyeIndex = EyeColourOptions.IndexOf(eyeColour);
                int skinIndex = SkinToneOptions.IndexOf(skinTone);

                // create proxy
                ColourTypeServiceClient client = new ColourTypeServiceClient();

                // Event handler for when web service call is finished. Event handler calls another method to display the result
                client.DetermineDominantCharacteristicCompleted += new EventHandler<DetermineDominantCharacteristicCompletedEventArgs>(client_DetermineDominantCharacteristicCompleted);
                
                // web service call, pass in user selection to method: 
                // hair and eye colour index to retrieve result from array and skin tone as string to determine which array to read result from
                client.DetermineDominantCharacteristicAsync(hairIndex, eyeIndex, skinIndex);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        // method to display result of web service call
        void client_DetermineDominantCharacteristicCompleted(object sender, DetermineDominantCharacteristicCompletedEventArgs e)
        {
            // if there are no errors 
            if (e.Error == null)
            {             
                try
                {
                    // Save to App class for isolated storage setting
                    (Application.Current as App).DominantCharacteristic = e.Result;
                    
                    // navigate to ColourTypePage where result will be displayed
                    NavigationService.Navigate(new Uri("/ColourTypePage.xaml", UriKind.Relative));
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                // display error message
                MessageBox.Show(e.Error.ToString());
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            // Navigate to previous page i.e. Intro page (MainPage)
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            // Navigate to next page which is page for result of dominant characterisitic
            NavigationService.Navigate(new Uri("/ColourTypePage.xaml", UriKind.Relative));
        }

        private void IntroItem_Click(object sender, EventArgs e)
        {
            // Navigate to Intro page i.e. MainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ColourTypeDescriptionItem_Click(object sender, EventArgs e)
        {
            //Navigate to My Colour Type Description page
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void ColourTypeImagesItem_Click(object sender, EventArgs e)
        {
            // Navigate to My Colour Type Images page
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }

        private void FashionTrendsItem_Click(object sender, EventArgs e)
        {
            // Navigate to Fashion Trends page
            NavigationService.Navigate(new Uri("/FashionTrendsPage.xaml", UriKind.Relative));
        }  
    }
}